﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using METODESA.Server.Data.METODESA.Entity;

namespace Metodesa.Tools
{
    public class ProveedorTools
    {
        public static HtmlTable ListProveedor(IList<Proveedor> lProveedor)
        {
            HtmlTable table = new HtmlTable();
            #region lProveedorHaveItemws

            if (lProveedor != null && lProveedor.Count != 0)
            {
                HtmlTableRow r = new HtmlTableRow();
                HtmlTableCell edit = new HtmlTableCell();
                HtmlTableCell details = new HtmlTableCell();
                HtmlTableCell delete = new HtmlTableCell();
                HtmlTableCell id = new HtmlTableCell();
                HtmlTableCell name = new HtmlTableCell();
                HtmlTableCell ci = new HtmlTableCell();
                HtmlTableCell address = new HtmlTableCell();
                HtmlTableCell[] telefono= new HtmlTableCell[3];
                
               
                details.BorderColor= id.BorderColor = edit.BorderColor = delete.BorderColor 
                                           = name.BorderColor = ci.BorderColor = "#FFFFFF";
                id.BgColor = edit.BgColor = name.BgColor = delete.BgColor = ci.BgColor=
                                               details.BgColor = address.BgColor = "#a9a9a9";
                delete.Controls.Add(new LiteralControl(""));
                edit.Controls.Add(new LiteralControl(""));
                details.Controls.Add(new LiteralControl(""));
                id.Controls.Add(new LiteralControl("<b>id</b>"));
                name.Controls.Add(new LiteralControl("<b>Nombre </b>"));
                ci.Controls.Add(new LiteralControl("<b>Cedula o Rif</b>"));
                address.Controls.Add(new LiteralControl("<b>Direccion</b>"));
                r.Cells.Add(details);
                r.Cells.Add(delete);
                r.Cells.Add(edit);
                r.Cells.Add(id);
                r.Cells.Add(name);
                r.Cells.Add(ci);
                r.Cells.Add(address);
                for (int cont = 0; cont < 3; cont++)
                {
                    telefono[cont]= new HtmlTableCell();
                    telefono[cont].BgColor = "#a9a9a9";
                    telefono[cont].BorderColor = "#FFFFFF";
                    int x = cont + 1;
                    telefono[cont].Controls.Add(new LiteralControl("Telefono" + x));
                    r.Cells.Add(telefono[cont]);
                }
                table.Rows.Add(r);
                int i = 0;
                foreach (Proveedor proveedor in lProveedor)
                {
                    i++;
                    details= new HtmlTableCell();
                    r = new HtmlTableRow();
                    id = new HtmlTableCell();
                    name = new HtmlTableCell();
                    ci = new HtmlTableCell();
                    address = new HtmlTableCell();
                    edit = new HtmlTableCell();
                    delete = new HtmlTableCell();
                    id.BorderColor = name.BorderColor = ci.BorderColor = "#FFFFFF";
                    if (i % 2 == 0)
                    {
                        details.BgColor=delete.BgColor = id.BgColor = edit.BgColor = name.BgColor = address.BgColor = ci.BgColor = "#E0E9F5";
                    }
                    else
                    {
                        details.BgColor=id.BgColor = delete.BgColor = name.BgColor = edit.BgColor = address.BgColor = ci.BgColor = "#D3D3D3";
                    }
                    
                    
                    id.Controls.Add(new LiteralControl(proveedor.Id + ""));
                    name.Controls.Add(new LiteralControl(proveedor.Nombre));
                    ci.Controls.Add(new LiteralControl(proveedor.CedulaRif));
                    address.Controls.Add(new LiteralControl(proveedor.Direccion));
                    HyperLink link = new HyperLink();

                    link.Text = "Editar";
                    link.NavigateUrl = "ListProveedor.aspx?a=s&id=" + proveedor.Id;
                    edit.Controls.Add(link);

                    HyperLink linkBorrar = new HyperLink();
                    linkBorrar.Text = "Borrar";
                    linkBorrar.NavigateUrl = "ListProveedor.aspx?a=d&id=" + proveedor.Id;
                    delete.Controls.Add(linkBorrar);

                    HyperLink linkDetails = new HyperLink();
                    linkDetails.Text = "Detalle";
                    linkDetails.NavigateUrl = "DetailsProveedor.aspx?id=" + proveedor.Id;
                    details.Controls.Add(linkDetails);

                    r.Cells.Add(details);
                    r.Cells.Add(delete);
                    r.Cells.Add(edit);
                    r.Cells.Add(id);
                    r.Cells.Add(name);
                    r.Cells.Add(ci);
                    r.Cells.Add(address);
                    int cont = 0;
                    foreach (Telefono tel in proveedor.telefono)
                    {
                        telefono[cont] = new HtmlTableCell();
                        telefono[cont].BgColor = id.BgColor;
                        telefono[cont].Controls.Add(new LiteralControl(tel.Numero));
                        
                        r.Controls.Add(telefono[cont]);
                        cont++;
                    }

                    for (; cont < 3;cont++ )
                    {
                        telefono[cont] = new HtmlTableCell();
                        telefono[cont].BgColor = id.BgColor;
                        telefono[cont].Controls.Add(new LiteralControl(""));
                        
                        r.Controls.Add(telefono[cont]);
                        
                    }

                        table.Rows.Add(r);

                }
            }
            #endregion
            else
            {
                return null;

            }




            return table;

        }
    }
}
